package com.yusheng.exceptions;

import com.yusheng.domain.R;
import com.yusheng.utils.ip.HttpUtils;
import jakarta.validation.ConstraintViolationException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * @ClassName jiang_jun_qin
 * @date 2023/1/31 17:28
 * @Version 1.0
 */
@RestControllerAdvice
@SuppressWarnings("all")
public class GlobalExceptionHandler {

    private static final Logger logger = LoggerFactory.getLogger(GlobalExceptionHandler.class);

    /**
     * 处理自定义的业务异常
     */
    @ExceptionHandler(value = BizException.class)
    public R bizExceptionHandler(BizException e) {
        logger.error("发生业务异常！原因是：", e);
        return R.fail(e.getCode(),e.getCode() + "::" + e.getMessage() + "!");
    }

    @ExceptionHandler(ServiceException.class)
    public R handleServiceException(ServiceException e) {
        logger.error("发生业务异常！原因是：", e);
        Integer code = e.getCode();
        return code != null ? R.fail(code, e.getMessage()) : R.fail(e.getMessage());
    }

    /**
     * 处理空指针的异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = NullPointerException.class)
    public R exceptionHandler(NullPointerException e) {
        logger.error("发生空指针异常！原因是:", e);
        return R.fail(ExceptionEnum.NULL_ERROR.getCode(), ExceptionEnum.NULL_ERROR.getMsg());
    }

    /**
     * @title exceptionHandler
     * @description 参数校验异常
     * @author Remy
     * @updateTime 2022/11/5 11:06
     */
    @ExceptionHandler(value = BindException.class)
    public R validatedBindException(BindException e) {
        logger.error("参数校验异常！原因是:", e.getMessage(), e);
        String message = e.getAllErrors().get(0).getDefaultMessage();
        return R.fail(ExceptionEnum.PARAM_ERROR.getCode(), message);
    }


    @ExceptionHandler(value = ConstraintViolationException.class)
    public R validatedBindException(ConstraintViolationException e) {
        logger.error("参数校验异常！原因是:", e.getMessage(), e);
        String message = e.getMessage();
        return R.fail(ExceptionEnum.PARAM_ERROR.getCode(), message);
    }


    /**
     * 请求方式不支持
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public R handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e) {
        String requestURI = HttpUtils.getRequest().getRequestURI();
        logger.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod());
        return R.fail(ExceptionEnum.BAD_METHOD.getCode(), e.getMessage());
    }

    /**
     * 自定义验证异常
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public R handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        logger.error(e.getMessage(), e);
        String message = e.getBindingResult().getFieldError().getDefaultMessage();
        return R.fail(ExceptionEnum.PARAM_ERROR.getCode(), message);
    }


    /**
     * 处理其他异常
     *
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value = Exception.class)
    public R exceptionHandler(Exception e) {
        logger.error("未知异常！原因是:", e);
        return R.fail(ExceptionEnum.INTERNAL_SERVER_ERROR.getCode(), ExceptionEnum.INTERNAL_SERVER_ERROR.getMsg());
    }
}
